package com.googlecode.accurev2disk;

public class ParseTransactions {

	private List transactions

	  ParseTransactions () {
	      transactions = new ArrayList()
	  }

	  

	/**
	   * parses the xml output of Accurev calculateHistory (accurev hist -a -s Stream -fx)
	   * @param xml .xml File to parse from Accurev
	   * @return all Transactions listed in the xml file
	   */
	public AccurevTransaction[] parseTransactions(File xml) {

	      return parseTransaction(xml.getText())
	      
	}

	/** takes the Transaction Numbers from an "accurev hist" xml output
	 * @param data XML output from accurev hist command
	 * @return List of the Transaction Numbers, where changes have been made
	 */
	public Integer[] getTransactionNumbers(String data) {

		def result = []
	      def xmlSlurper = new XmlSlurper()
	      def transactionsXml = xmlSlurper.parseText(data)

	      //	save ID from every <transaction> to the result Integer list
	      transactionsXml.transaction.each {
				result.add(Integer.parseInt(it.@id.text()))
	      }
			return result
	      }
	

	/**
	   * parses the xml output of Accurev calculateHistory (accurev hist -a -s Stream -fx)
	   * ignores the <version>s
	   * @param xml .xml File to parse from Accurev
	   * @return all Transactions listed in the xml file without their contained versions
	   */
	public AccurevTransaction[] parseTransactions(String data) {
		
	      def xmlSlurper = new XmlSlurper()
	      def transactionsXml = xmlSlurper.parseText(data)

	      //	save information for every <transaction> to an AccurevTransaction object
	      transactionsXml.transaction.each {
	    	  
	          def transaction = new AccurevTransaction()
	          // save attributes id,type,time,user as Strings in the Transaction
	          transaction.id = it.@id.text()
	          transaction.type = it.@type.text()
	          transaction.time = it.@time.text()
	          transaction.user = it.@user.text()
	          transaction.comment = it.comment.text()

	          transactions.add(transaction)
	      }
	      return (AccurevTransaction[])transactions.toArray()
	  }

	/**
	   * parses the xml output of Accurev calculateHistory (accurev hist -a -s Stream -fx)
	   * @param xml .xml File to parse from Accurev
	   * @return all Transactions listed in the xml file
	   */
	public AccurevTransaction[] parseTransactionsWithVersions(File xml) {

		  //	slurp xml file
	      def data = xml.getText()
	      def xmlSlurper = new XmlSlurper()
	      def transactionsXml = xmlSlurper.parseText(data)

	      //	save information for every <transaction> to an AccurevTransaction object
	      transactionsXml.transaction.each {
	    	  
	          def transaction = new AccurevTransaction()
	          // save attributes id,type,time,user as Strings in the Transaction
	          transaction.id = it.@id.text()
	          transaction.type = it.@type.text()
	          transaction.time = it.@time.text()
	          transaction.user = it.@user.text()
	          transaction.comment = it.comment.text()
	          
	          // several Files per Transaction lead to several <version>...</version> tags
	          // create an AccurevTVersion for each and save the list in the actual Transaction
	          
	          it.version.each{	      
	        	  
		          def version = new AccurevTVersion()
		          
		          version.path = it.@path.text()
		          version.eid = it.@eid.text()
		          version.virtual = it.@virtual.text()
		          version.real = it.@real.text()
		          version.virtualNamedVersion = it.@virtualNamedVersion.text()
		          version.realNamedVersion = it.@realNamedVersion.text()
		          version.elem_type = it.@elem_type.text()
		          version.dir = it.@dir.text()
		          
		          transaction.versions.add(version)
	          }

	          transactions.add(transaction)
	      }
	      return (AccurevTransaction[])transactions.toArray()
	  }
}
